package org.ank.food;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class YourHeightActivity extends Activity{
	
	Button[] numPadButtons = new Button[11];
	Button backButton,nextButton,cancelButton;
	TextView textView;
//	NumPadHandler npHandler;
	RadioGroup unitRadioGroup;
	EditText editText;
	ProfileManager profileManager;
	@Override
	public void onCreate(Bundle b){
		super.onCreate(b);
		setContentView(R.layout.your_height_activity);
//		numPadButtons[0]=(Button)findViewById(R.id.your_height_0_button);
//		numPadButtons[1]=(Button)findViewById(R.id.your_height_1_button);
//		numPadButtons[2]=(Button)findViewById(R.id.your_height_2_button);
//		numPadButtons[3]=(Button)findViewById(R.id.your_height_3_button);
//		numPadButtons[4]=(Button)findViewById(R.id.your_height_4_button);
//		numPadButtons[5]=(Button)findViewById(R.id.your_height_5_button);
//		numPadButtons[6]=(Button)findViewById(R.id.your_height_6_button);
//		numPadButtons[7]=(Button)findViewById(R.id.your_height_7_button);
//		numPadButtons[8]=(Button)findViewById(R.id.your_height_8_button);
//		numPadButtons[9]=(Button)findViewById(R.id.your_height_9_button);
//		numPadButtons[10]=(Button)findViewById(R.id.your_height_backspase_button);
		
//		textView = (TextView)findViewById(R.id.your_height_text_view);
		editText = (EditText)findViewById(R.id.your_height_text_view);
		
//		npHandler = new NumPadHandler(numPadButtons,textView,getApplicationContext());
		
		unitRadioGroup = (RadioGroup)findViewById(R.id.your_height_unit_radio_group);
		
		nextButton = (Button)findViewById(R.id.your_height_next_button);
		backButton = (Button)findViewById(R.id.your_height_back_button);
		cancelButton = (Button)findViewById(R.id.your_height_cancel_button);
		
		profileManager = ProfileManager.getInstance(getApplicationContext());
		int a;
		if ( (a = profileManager.getNewProfileHeight()) != 0 ){
			editText.setText(String.valueOf(a));
		}else if (profileManager.getProfileMode() == ProfileManager.PROFILE_MODE_EDIT){
			editText.setText(String.valueOf(profileManager.getCurrentProfileHeight()));
		}
		nextButton.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				String name = editText.getText().toString();
				if ( name.equals("") ){
					Toast.makeText(getApplicationContext(), R.string.your_height_specify_height,
							Toast.LENGTH_SHORT).show();
					return;
				}
				if (unitRadioGroup.getCheckedRadioButtonId() == R.id.your_height_unit_radio_button_metric)
					ProfileManager.getInstance( getApplicationContext() ).setNewProfileHeight(
							Integer.parseInt(editText.getText().toString()));
				else
					ProfileManager.getInstance(getApplicationContext()).setNewProfileHeight(
							(int) Math.round( Integer.parseInt(editText.getText().
									toString())*ProfileManager.IN_TO_CM ) );
				Intent intent = new Intent();
        		intent.setClass(getApplicationContext(), PhysicalActivityActivity.class);
        		intent.putExtra("caller", PhysicalActivityActivity.ALPHA_VERSION_HEIGHT_ACTIVITY);//only for alpha
        		intent.putExtra(EnterNameActivity.CALL_FROM_FOLLOWING, false);
        		startActivity(intent);
        		finish();
			}
		});
		backButton.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				Intent intent = new Intent();
        		intent.setClass(getApplicationContext(), YourWeightActivity.class);
        		intent.putExtra(EnterNameActivity.CALL_FROM_FOLLOWING, true);
        		startActivity(intent);
        		finish();
			}
		});
		cancelButton.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				AlertDialog.Builder builder = new AlertDialog.Builder(YourHeightActivity.this);
//				builder.setMessage(getString(R.string.profile_create_alert_dialog_message));
				builder.setNegativeButton(R.string.profile_create_alert_dialog_negative_button_text, 
						new DialogInterface.OnClickListener(){
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});
				builder.setPositiveButton(R.string.profile_create_alert_dialog_positive_button_text, 
						new DialogInterface.OnClickListener(){
						@Override
						public void onClick(DialogInterface dialog,	int which) {
							Intent intent = new Intent();
			        		intent.setClass(getApplicationContext(), ProfileChooseActivity.class);
			        		startActivity(intent);
			        		finish();
						}
					});
				builder.setTitle(R.string.profile_create_alert_dialog_title);
				builder.show();
			}
			
		});
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), YourWeightActivity.class);
			startActivity(intent);
			finish();
			return true;
		}
	    return super.onKeyDown(keyCode, event);
	}
}